package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.CaseMapper;
import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.dto.resp.CaseResponseDTO;
import com.winshare.saleAssistant.domain.entity.CaseDO;
import com.winshare.saleAssistant.domain.entity.StatisticsDO;
import com.winshare.saleAssistant.domain.query.CaseQuery;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.CaseMapStruct;
import com.winshare.saleAssistant.service.CaseService;
import com.winshare.saleAssistant.util.Page;
import com.winshare.saleAssistant.util.RequestHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CaseServiceImpl implements CaseService {

    private final CaseMapper caseMapper;

    public CaseServiceImpl(CaseMapper caseMapper) {
        this.caseMapper = caseMapper;
    }

    @Override
    public void save(CaseCreateDTO caseCreateDTO) {
        CaseDO caseDO = CaseMapStruct.INSTANCE.fromCaseCreateDTO(caseCreateDTO);
        caseDO.setCreatorId(RequestHolder.getCurrentUser().getId());
        caseDO.setStatus(1);
        caseDO.setCreator(RequestHolder.getCurrentUser().getStaffName());
        caseDO.setOrgId(RequestHolder.getCurrentUser().getOrgId());
        caseDO.setOrgName(RequestHolder.getCurrentUser().getOrgName());
        caseMapper.saveCase(caseDO);

        // 统计
//        StatisticsDO statisticsDO = new StatisticsDO();
//        statisticsDO.setArticleSubmitCount(1);
//        statisticsDO.setType1("文章");
//        statisticsDO.setObjId(articleDOInSql.getId());
//        statisticsDO.setObjName(articleDOInSql.getArticleTitle());
//        statisticsDO.setArticleSubmitCount(1);
//        statisticsService.saveStatistic(statisticsDO);
    }

    @Override
    public void reply(CaseReplyDTO caseReplyDTO) {
        CaseDO caseDO = CaseMapStruct.INSTANCE.fromCaseReplyDTO(caseReplyDTO);
        caseMapper.updateCase(caseDO);
    }

    @Override
    public void setRead(CaseIdListDTO caseIdListDTO) {
        caseMapper.setRead(caseIdListDTO);
    }

    @Override
    public Page<CaseResponseDTO> getCaseList(CaseQueryDTO caseQueryDTO) {
        CaseQuery caseQuery = CaseMapStruct.INSTANCE.fromCaseQueryDTO(caseQueryDTO);
        caseQuery.setOffsetIndex(caseQueryDTO.getOffsetIndex());
        int totalCount = caseMapper.queryCaseCount(caseQuery);
        Integer pageIndex = caseQueryDTO.getPageIndex();
        Integer pageSize = caseQueryDTO.getPageSize();
        if(caseQueryDTO.getIsCurrent().intValue() == 1) {
            caseQuery.setCreatorId(RequestHolder.getCurrentUser().getId());
        }
        if(0 == totalCount) {
            return new Page<>(pageIndex,pageSize,totalCount,new ArrayList<>());
        }
        List<CaseDO> caseDOList = caseMapper.findCaseListByConditions(caseQuery);
        List<CaseResponseDTO> caseResponseDTOList = CaseMapStruct.INSTANCE.fromCaseDOList(caseDOList);
        return new Page<>(pageIndex,pageSize,totalCount,caseResponseDTOList);
    }

    @Override
    public CaseResponseDTO getCaseDetail(BaseIdDTO baseIdDTO) {
        CaseDO caseDO = CaseMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        CaseDO CaseDOInSql = caseMapper.getCase(caseDO);
        if(null == CaseDOInSql) {
            throw new BusinessException("当前案例不存在。");
        }
        CaseResponseDTO caseResponseDTO = CaseMapStruct.INSTANCE.fromArticleDO(CaseDOInSql);
        return caseResponseDTO;
    }

    @Override
    public Page<CaseResponseDTO> getMyCaseList(CaseQueryDTO caseQueryDTO) {
        return null;
    }

    @Override
    public void setAdopted(CaseIdListDTO caseIdListDTO) {
        caseMapper.setAdopted(caseIdListDTO);
    }
}

